//OPTBEDIT.CPP
#include<ctype.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>

// To create the opcode table
// The opcode table is organized as binary search tree
// for efficient searching

class node
{  private:
    char mnemonic[6];
    char opcode[3];
    int left,right;
   public:
    node()  			{   left=0;   right=0;   }
    void setmnemonic(char* x)   {   strcpy(mnemonic,x);  }
    void setopcode(char *x)	{   strcpy(opcode,x);   }
    void setleft(int x)   	{ left=x;  }
    void setright(int x)	{ right=x; }
    void indata(int i)
     {
      gotoxy(2,i+4);    cin>>mnemonic;
      gotoxy(25,i+4);   cin>>opcode;
      left=0;    right=0;
     }
    char* retmnemonic()		{ return mnemonic;   }
    char* retopcode()		{ return opcode;  }
    int retleft()	        { return left;}
    int retright()		{ return right; }

    void outdata(int i)
     {
      gotoxy(2,i+4);     cout<<mnemonic;
      gotoxy(18,i+4);    cout<<opcode;
     }
}; // end of class

void inorder(int ptr)
{
 ifstream os;
 node t;
 int temp;
 static int i=0;

 os.open("optab.dat",ios::in|ios::binary);
 if(ptr!=0)
  {
   os.seekg(ptr*sizeof(node),ios::beg); os.read((char*)&t,sizeof(t));
   temp=t.retleft();   inorder(temp);   t.outdata(i++);
   temp=t.retright();  inorder(temp);
  }
}

int searchop(char str[])
{ifstream os;
 node t;
 int temp,ptr=1;

 os.open("optab.dat",ios::in|ios::binary); os.seekg(0,ios::beg);
 os.read((char*)&t,sizeof(t));
 if(t.retleft()==0)  { os.close();  return 0; }

 while(ptr!=0)
 { os.seekg(ptr*sizeof(node),ios::beg);
   os.read((char*)&t,sizeof(t));
   if(strcmpi(str,t.retmnemonic()) < 0)    {   ptr=t.retleft();    }
   else if(strcmpi(str,t.retmnemonic()) == 0)
     {  strcpy(str,t.retopcode());      os.close();      return 1;     }
   else   { ptr=t.retright(); }
 }  // end of while loop

 os.close();
 return 0;
} // end of search

